import entity.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ttt {
    private static ApplicationContext ac;
    static {
        ac = new ClassPathXmlApplicationContext("spring.xml");
    }

    public void c(){
        SupplierService s = (SupplierService) ac.getBean("supplierService");
        List<Supplier> a = s.list();
        for (Supplier q :a){
            System.out.println(q.getName());
        }
    }

    public void w(){
        WarehouseService s = (WarehouseService) ac.getBean("warehouseService");
        List<Warehouse> a = s.list();
        for (Warehouse q :a){
            System.out.println(q.getName());
        }
    }
    public void che(){
        LinService s = (LinService) ac.getBean("linService");
        List<Lin> a = s.list2();


        for (Lin q :a){
            System.out.println(q.getColor().getName());
        }
    }
    public void del(){
        LinService s = (LinService) ac.getBean("linService");
        int i = s.del(32);


        System.out.println(i);
    }

    public void cha(){
        StaffService s = (StaffService) ac.getBean("staffService");
        List<Staff> list = s.listjin();

        for (Staff s1:list){
            System.out.println(s1.getName());
        }


    }
    public void yan(){
        ColortypeService s = (ColortypeService) ac.getBean("colortypeService");
        List<Colortype> list = s.list();

        for (Colortype s1:list){
            System.out.println(s1.getName());
        }


    }

    public void yan2(){
        UserService s = (UserService) ac.getBean("userService");

        User u = new User();
        u.setName("admin");
        u.setPwd("123123");
        int i = s.cha(u);

        System.out.println(i);


    }


    public void l(){

        UserService userService = (UserService) ac.getBean("userService");
        LinService linService = (LinService) ac.getBean("linService");
        CommodityService commodityService = (CommodityService) ac.getBean("commodityService");


        List<Lin> list = linService.list3();
        Commodity c = null;

        for(int i = 0;i<list.size();i++){


            c = new Commodity();

            c.setNumber(list.get(i).getNumber());
            c.setWarehouseid(2);



            c = commodityService.cha(c);

            System.out.println(c==null);




            if(c==null){
                c = new Commodity();

                c.setNumber(list.get(i).getNumber());

                System.out.println(c.getNumber());

                c.setName(list.get(i).getName());

                c.setCompanyid(list.get(i).getCompanyid());


                c.setSpec(list.get(i).getSpec());
                c.setColorid(list.get(i).getColorid());
                c.setBprice(list.get(i).getBprice());
                c.setEprice(list.get(i).getBprice()+1);
                c.setStock(list.get(i).getSum());
                c.setWarehouseid(2);



                int j = commodityService.add(c);
                if(j!=0){
                    System.out.println("加成功");
                }else{
                    System.out.println("加失败");
                }

            }else{
                c.setBprice(list.get(i).getBprice());
                c.setStock(c.getStock()+list.get(i).getSum());

                int j = commodityService.upd(c);
                if(j!=0){
                    System.out.println("改成功");
                }else{
                    System.out.println("改失败");
                }
            }

        }
    }




    public static void main(String[] args) {
        ttt t = new ttt();
        t.l();
    }

}
